﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using M7;
using System.Threading;
using System.Windows.Documents;

namespace C7
{
    public static class Chat
    {
        public static bool IsConnected = false;
        public delegate void delReceiveMsg(string Message);
        public static void Connect()
        {
            Msg SendMsg = new Msg(Properties.Settings.Default.Port, Properties.Settings.Default.TableID);
            ChatClient chatClient = new ChatClient(null, "Client", Info.GetLocalIP(), Properties.Settings.Default.TableID);
            SendMsg.obj = chatClient;
            SendMsg.enumMethod = enumChat.ChatConnect;
            TcpConnection.SendingQueue.Enqueue(SendMsg);
        } 
        public static void Disconnect()
        {
            Msg SendMsg = new Msg(Properties.Settings.Default.Port, Properties.Settings.Default.TableID);
        }
        public static void SendMsg(string Message)
        {
            Msg SendMsg = new Msg(Properties.Settings.Default.Port, Properties.Settings.Default.TableID);
            SendMsg.enumMethod = enumChat.ChatSend;
            var customerNames = from cust in Info.current.customers
                                where cust.CustomerID.Equals(Info.current.CustomerID)
                                select cust.Username;
            ChatClient chatClient = new ChatClient(Message, "Client", Info.GetLocalIP(), Properties.Settings.Default.Port);
            if (customerNames.ToList().Count != 0)
            {
                chatClient.name = customerNames.ToList()[0];
            }
            SendMsg.obj = chatClient;
            TcpConnection.SendingQueue.Enqueue(SendMsg);
        }

        public static void ReceiveMsg(ChatClient c)
        {
            if (Info.module is ChatModule)
            {
                (Info.module as ChatModule).tbChat.Dispatcher.Invoke(new ThreadStart(() =>
                {
                    (Info.module as ChatModule).tbChat.Items.Add(c);
                }));
            }
        }
    }
}
